package com.ltmonitor.web.controller.command;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.web.util.JsonMessage;
/**
 * 车门控制
 * @author DELL
 *
 */
@Controller
@RequestMapping("/doorControl")
public class DoorControlAction extends TerminalCommandAction {

	@RequestMapping("/view.action")
	public String view(long vehicleId, Model m)
	{
		m.addAttribute("vehicleId", vehicleId);
		return "/command/doorControl";
	}

	/**
	 *  门控制
	 * @param controlType 控制类型
	 * @param switchState 开关状态  0 开 1关
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage execute(long vehicleId, int controlType,int switchState) {

		int[] bitValues = new int[8];
		if(controlType < 8)
		{
			bitValues[controlType] = switchState;
		}
		StringBuilder strBit=new StringBuilder();
		ArrayUtils.reverse(bitValues);
		for(int bit : bitValues)
			strBit.append(bit);
		byte bitValue = Byte.valueOf(strBit.toString(), 2); //将位数组转换为字节值
		
		TerminalCommand tc = new TerminalCommand();
		tc.setCmdType(JT808Constants.CMD_CONTROL_VEHICLE);
		tc.setCmdData(""+bitValue);
		tc.setCmd("" + bitValue);
		tc.setVehicleId(vehicleId);
		SendCommand(tc);

		return json(true, tc.getEntityId());
	}


}
